"""
链家郑州二手房https://zz.lianjia.com/ershoufang/rs/
"""

import requests
from pyquery import PyQuery as pq
import time

url = "https://zz.lianjia.com/ershoufang/rs/"
res = requests.get(url)
# print(res.text)
query = pq(res.text)
areas = query("body > div:nth-child(12) > div > div.position > dl:nth-child(2) > dd > div > div > a")
# print(len(areas))
for area in areas:
    time.sleep(1)
    area = pq(area)
    area_link = "https://zz.lianjia.com" + area("a").attr("href")
    area_name = area("a").text()
    # print(area_name, area_link)
    area_res = requests.get(area_link)
    area_query = pq(area_res.text)
    # print(area_res.text)
    # 劲爆！客厅朝南带阳台。
    houses = area_query(".sellListContent li")
    count = 1
    for house in houses:
        time.sleep(1)
        house_query = pq(house)
        house_url = house_query("li .info .title a").attr("href")
        house_res = requests.get(house_url)
        house_query = pq(house_res.content.decode())
        # print(house_res.content.decode())
        title = house_query(".sellDetailHeader .title-wrapper .content .title h1").text()
        price = house_query(".overview .content .price-container .price .total,.unit").text()
        unit_price = house_query(".overview .content .price-container .price .text .unitPrice").text()
        room = house_query(".overview .content .houseInfo .room").text()
        house_type = house_query("#introduction > div > div > div.base > div.content > ul > li:nth-child(1)").text()
        house_area = house_query(".overview .content .houseInfo .area").text()
        community = house_query(".overview .content .aroundInfo .communityName span+a").text()
        area_name = house_query(".overview .content .aroundInfo .areaName .info a").text()
        print(area_name, title, price, unit_price, room, house_type, house_area, community, area_name)
        print("===================================================================")
        count += 1
        if count >= 10:
            break
        # break


    # break